package com.wn.class30.service.impl;

import com.wn.class30.entity.*;
import com.wn.class30.mapper.*;
import com.wn.class30.service.ShoppingOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class ShoppingOrderServiceImpl implements ShoppingOrderService {
    @Resource
    private UserAddressDao userAddressDao;

    @Override
    public UserAddress getAddressByUserId(Integer userId) {
        UserAddressExample example = new UserAddressExample();
        UserAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andIsdefaultEqualTo(1);
        List<UserAddress> userAddresses = userAddressDao.selectByExample(example);
        if(userAddresses != null && userAddresses.size()!=0){

            return userAddresses.get(0);
        }
        return null;
    }

    @Override
    public int deleteUserAddress(Integer userAddressId) {
        return userAddressDao.deleteByPrimaryKey(userAddressId);
    }

    @Override
    public int updateUserAddressAllDefaultToZero(Integer userid) {
        UserAddressExample example = new UserAddressExample();
        UserAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userid);
        UserAddress address = new UserAddress();
        address.setIsdefault(0);
        return userAddressDao.updateByExampleSelective(address,example);
    }

    @Override
    public int updateUserAddressDefaultToOne(Integer userAddressId) {
        UserAddressExample example = new UserAddressExample();
        UserAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserAddressIdEqualTo(userAddressId);
        UserAddress address = new UserAddress();
        address.setIsdefault(1);
        return userAddressDao.updateByExampleSelective(address,example);
    }

    @Resource
    IntegralDao integralDao;
    @Override
    public Integral getIntegral() {
        return integralDao.selectByExample(null).get(0);
    }

    @Resource
    AllorderDao allorderDao;
    @Override
    public Integer insertAllOrder(Allorder allorder) {
        return allorderDao.insert(allorder);
    }

    @Resource
    MyorderDao myorderDao;
    @Override
    public Integer insertChildOrder(Myorder myorder) {
        return myorderDao.insert(myorder);
    }

    @Resource
    MyorderDetailDao myorderDetailDao;

    @Override
    public Integer insertOrderDetail(MyorderDetail myorderDetail) {
        return myorderDetailDao.insert(myorderDetail);
    }
    
    @Resource
    UserCuponDao userCuponDao;
    //修改或者删除
    @Override
    public Integer updateCuponCount(Integer ucId) {
        if(ucId !=null){

        UserCupon userCupon = userCuponDao.selectByPrimaryKey(ucId);
        if (userCupon.getCuponNum() == 1){
            //删除优惠券
            System.out.println(userCupon);
            userCuponDao.deleteByPrimaryKey(userCupon.getUcId());
        }else{
            //减少数量
            System.out.println(userCupon);
            userCupon.setCuponNum(userCupon.getCuponNum() - 1);
            UserCuponExample example = new UserCuponExample();
            UserCuponExample.Criteria criteria = example.createCriteria();
            criteria.andUcIdEqualTo(userCupon.getUcId());
            userCuponDao.updateByExampleSelective(userCupon,example);
        }

        }
        return null;
    }

    @Override
    public Integer updateChildOrderStatus(Integer getAllorderId) {
        Myorder myorder = new Myorder();
        myorder.setMyorderStatus(1);
        MyorderExample example = new MyorderExample();
        MyorderExample.Criteria criteria = example.createCriteria();
        criteria.andAllorderIdEqualTo(getAllorderId);
        return myorderDao.updateByExampleSelective(myorder, example);
    }

    @Resource
    UserDao userDao;
    @Override
    public Integer updateUserPoints(Integer userid, Integer getAllorderId) {
        //查询订单项积分和、增加用户积分。
        //子订单
        MyorderExample example = new MyorderExample();
        MyorderExample.Criteria criteria = example.createCriteria();
        criteria.andAllorderIdEqualTo(getAllorderId);
        List<Myorder> myorders = myorderDao.selectByExample(example);
        //订单项
        List<MyorderDetail> md = new ArrayList<MyorderDetail>();
        for (Myorder myorder : myorders) {
            int myorderId = myorder.getMyorderId();
            MyorderDetailExample example1 = new MyorderDetailExample();
            MyorderDetailExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andMyorderIdEqualTo(myorderId);
            List<MyorderDetail> myorderDetails = myorderDetailDao.selectByExample(example1);
            for (MyorderDetail myorderDetail : myorderDetails) {
                md.add(myorderDetail);
            }
        }
        //获取成长值
        Integer points = 0;
        for (MyorderDetail myorderDetail : md) {
            points += Integer.parseInt(myorderDetail.getOdIntegral());
        }
        System.out.println("成长值"+points);
        User user = userDao.selectByPrimaryKey(userid);
        user.setUserId(userid);
        user.setPoints(user.getPoints()+points);
        return userDao.updateByPrimaryKeySelective(user);
    }

    @Resource
    ShoppingcarDao shoppingcarDao;
    @Override
    public Integer deleteCarInOrder(Integer userid, Integer[] priduct_ids) {
        for (Integer priduct_id : priduct_ids) {
            ShoppingcarExample example = new ShoppingcarExample();
            ShoppingcarExample.Criteria criteria = example.createCriteria();
            criteria.andProductIdEqualTo(priduct_id);
            criteria.andUserIdEqualTo(userid);
            shoppingcarDao.deleteByExample(example);
        }
        return null;
    }
}
